import multiprocessing

from sqlalchemy import create_engine
from config import MYSQL_DB, MYSQL_PASSWORD, MYSQL_HOST, MYSQL_PORT, MYSQL_USER
from sqlalchemy.orm import scoped_session, sessionmaker
import urllib.parse

class CreateEngine(object):

    # @property
    # def _get_contract_manage_mysql_url(self):
    #     mysql_url = f'mysql+pymysql://{MYSQL_USER}:{MYSQL_PASSWORD}@{MYSQL_HOST}' \
    #                 f':{MYSQL_PORT}/{MYSQL_DB}?' \
    #                 f'charset=utf8mb4'
    #     return mysql_url

    @property
    def _get_contract_manage_mysql_url(self):
        MYSQL_PASSWORD_1 = urllib.parse.quote_plus(MYSQL_PASSWORD)
        mysql_url = f'mysql+pymysql://{MYSQL_USER}:{MYSQL_PASSWORD_1}@{MYSQL_HOST}' \
                    f':{MYSQL_PORT}/{MYSQL_DB}?' \
                    f'charset=utf8mb4'
        return mysql_url


    @property
    def get_mysql_url(self):
        return {
            "label": self._get_contract_manage_mysql_url,
        }

    def get_engine(self, method="label"):
        engine = create_engine(self.get_mysql_url[method], pool_size=multiprocessing.cpu_count() * 1,
                               pool_recycle=28800)
        return scoped_session(sessionmaker(bind=engine, autoflush=True, autocommit=True))
